<?php
namespace Admin\Controller;
use Think\Controller;
/**
* ContentController
*/
class ContentController extends CommonController
{
	//树形菜单页
	public function index()
	{
		$category = M('Category')->field('cateid,modelid,catename,parentid,is_final')->where('modelid>0')->select();
		$list = new \Admin\Model\ContentModel();
		$lists = $list->make_tree1($category);
		$jsonarr = json_encode($lists);
		$this->assign('tree',$jsonarr);
		$this->display();
	}
	//内容列表
	public function cateContent($page = 1)
	{
		//模型ID
		$modelid = I('get.modelid');
		if (empty($modelid)) {
			$this->error("请指定模型！");
		}
		//栏目ID
		$cateid = I('get.cateid');
		if (empty($cateid)) {
			$this->error("请指定栏目！");
		}
		$table = M('Model')->field('table')->where("modelid=$modelid")->find();
		$tablename = 'model_'.$table['table'];
		$listcount = M($tablename)->where(array('modelid'=>$modelid,'catid'=>$cateid))->count();	//内容总数
		$count = M('category')->where('cateid='.$cateid)->getField('pagesize');		//每页条数
		$pages = ceil($listcount/$count);		//分页数量
		$list = M($tablename)->where(array('modelid'=>$modelid,'catid'=>$cateid))->limit(($page-1)*$count,$count)->select();
		foreach ($list as $k => $v) {
			$list[$k]['push'] = explode(',',$v['push']);
			$m=MODULE_NAME;
			if(strstr($list[$k]['url'],"index?")){
			    //动态地址
			    if($m == 'Home' || $m == 'Admin'){
			        $list[$k]['url'] ='/Home'.$list[$k]['url'];
			    }else{
			        $list[$k]['url'] ='/Mobile'.$list[$k]['url'];
			    }
			} else {
				$list[$k]['url'] ='/www'.$list[$k]['url'];
			}
		}
		$this->assign('tablename',$tablename);
		$this->assign('list',$list);
		$this->assign('cateid',$cateid);
		$this->assign('modelid',$modelid);
		$this->assign('pages',$pages);
		$this->assign('page',$page);
		$this->display();
	}
	//内容添加
	public function add()
	{
		//模型ID
		$modelid = I('get.modelid');
		if (empty($modelid)) {
			$this->error("请指定模型！");
		}
		//栏目ID
		$cateid = I('get.cateid');
		if (empty($cateid)) {
			$this->error("请指定栏目！");
		}
		if (IS_POST) {
			$post = I('post.');
			if (!empty($post['addtime'])) {
				$post['addtime'] = strtotime($post['addtime']);
			} else {
				$post['addtime'] = time();
			}
			$post['updatetime'] = "";
			$post['hits'] = 0;
			$post['push'] = implode(',', $post['push']);
			$post['relation'] = implode(',',$post['relation']);
			$table = M('Model')->field('table')->where("modelid=$modelid")->find();
			$tablename = 'model_'.$table['table'];
			$res = M($tablename)->add($post);
			if ($res) {
				$depr = M('cache')->where("name='HOME_URL_PATHINFO_DEPR'")->getField('value');
				$arr['id'] = $res;
				$pathname = M('category')->where('cateid='.$cateid.' AND modelid='.$modelid)->getField('pathname');
				$arr['url'] = '/show/index?pathname='.$pathname.'&id='.$res;
				M($tablename)->save($arr);
				alertMes('发布成功',"/admin/content/cateContent/modelid/$modelid/cateid/$cateid");
			} else {
				alertMes('发布失败');
			}
		} else {
			$catename = M('Category')->field('catename')->where("cateid=$cateid")->find();
			$this->assign('catename',$catename['catename']);

			$fielddata = D('Field')->where('disabled=0')->getModelField($modelid);
			foreach ($fielddata as $key => $value) {
				$data[$key] = D('Field')->priviewform($value);
			}
			$this->assign('dataform',$data);
			$this->assign('cateid',$cateid);
			$this->assign('modelid',$modelid);
			$this->display();
		}
	}
	//内容编辑
	public function edit()
	{
		//模型ID
		$tablename = I('get.tablename');
		$id = I('get.id');
		$list = M($tablename)->where('id='.$id)->find();
		$depr = M('cache')->where("name='HOME_URL_PATHINFO_DEPR'")->getField('value');
		if (IS_POST) {
			$post = I('post.');
			if (!empty($post['addtime'])) {
				$post['addtime'] = strtotime($post['addtime']);
			} else {
				unset($post['addtime']);
			}
			$post['updatetime'] = time();
			$post['push'] = implode(',', $post['push']);
			$post['relation'] = implode(',',$post['relation']);
			$post['url'] = $tablename.$depr.'id'.$depr.$id;
			$res = M($tablename)->where('id='.$id)->save($post);
			if ($res) {
				alertMes('编辑成功',"/admin/content/cateContent/modelid/{$list['modelid']}/cateid/{$list['catid']}");
			} else {
				alertMes('编辑失败');
			}
		} else {
			$catename = M('Category')->field('catename')->where("cateid={$list['catid']}")->find();
			$this->assign('catename',$catename['catename']);
			$fielddata = D('Field')->where('disabled=0')->getModelField($list['modelid']);
			foreach ($fielddata as $key => $value) {
				$value['value_info'] =  $list[$value['field']];
				$data[$key] = D('Field')->priviewform($value);
			}
			$this->assign('list',$list);
			$this->assign('dataform',$data);
			$this->assign('tablename',$tablename);
			$this->assign('id',$id);
			$this->display();
		}
	}
	//内容删除
	public function del()
	{
		$tablename = I('get.tablename');
		$id = I('get.id');
		$arr = M($tablename)->where('id='.$id)->field('modelid,catid,thumb')->find();
		$thumb = explode(',', $arr['thumb']);
		foreach ($thumb as $key => $value) {
			M('Files')->delete($value);
		}
		$res = M($tablename)->delete($id);
		if ($res) {
			alertMes('删除成功',"/admin/content/cateContent/modelid/{$arr['modelid']}/cateid/{$arr['catid']}");
		} else {
			alertMes('删除失败');
		}
	}
	//内容转移
	public function move()
	{
		$tablename = I('get.tablename');
		$id = I('get.id');
		$table = M($tablename)->where('id='.$id)->field('modelid,catid')->find();
		if (IS_POST) {
			$content['catid'] = I('post.cateid');
			$res = M($tablename)->where('id='.$id)->save($content);
			if ($res) {
				alertMes('移动成功',"/admin/content/cateContent/modelid/{$table['modelid']}/cateid/{$table['catid']}");
			} else {
				alertMes('移动失败');
			}
		} else {
			$arr = M($tablename)->where('id='.$id)->field('modelid,catid')->find();
			$cate = M('category')->where('cateid='.$arr['catid'])->field('catename')->find();
			$this->assign('catename',$cate['catename']);

			$list = M('category')->field("cateid,catename,parentid,arrparentid,concat(arrparentid,',',cateid) AS bpath")->order('bpath,cateid')->where('modelid='.$arr['modelid'].' AND is_final=1')->select();
			foreach ($list as $key => $value) {
				$list[$key]['signnum'] = count(explode(',', $value['bpath'])) - 1;
				$list[$key]['marginnum'] = (count(explode(',', $value['bpath'])) - 1) * 20;
			}
			$this->assign('id',$id);
			$this->assign('tablename',$tablename);
			$this->assign('milist',$list);
			$this->display();
		}
	}
	//相关文章
	public function relation()
	{
		$id = I('get.id');
		$tablename = I('get.tablename');
		if (empty($tablename) && !empty($id)) {
			echo "1";
		}
		if (!empty($id)) {
			$relation = M($tablename)->where('id='.$id)->getField('relation');
			$rela = explode(',', $relation);
			if (count($rela) > 1) {
				foreach ($rela as $key => $value) {
					$con = explode('-',$value);
					$table = M('model')->where('modelid='.$con[0])->getField('table');
					$tablename1 = 'model_'.$table;
					$title = M($tablename1)->where('id='.$con[1])->getField('title');
					$str1 .= "<div id='".$value."' style=' float:left; line-height:24px; border-bottom:dashed 1px #ccc;font-size:16px; margin:0 5% 0 0 ; padding-left:10px;'><span>".$title."</span><a href='javascript:;' onclick='removediv(this)' style='float:right; color:#ccc; padding-left:10px; padding-right:10px; font-size:12px'>X</a><input type='hidden' name='relation[]' value='".$value."'></div>";
				}
			} elseif ($relation == '') {
				alertMes('没有相关文章');
			} else {
				$con = explode('-',$relation);
				$table = M('model')->where('modelid='.$con[0])->getField('table');
				$tablename1 = 'model_'.$table;
				$title = M($tablename1)->where('id='.$con[1])->getField('title');
				$str1 = "<div id='".$relation."' style=' float:left; line-height:24px; border-bottom:dashed 1px #ccc;font-size:16px; margin:0 5% 0 0 ; padding-left:10px;'><span>".$title."</span><a href='javascript:;' onclick='removediv(this)' style='float:right; color:#ccc; padding-left:10px; padding-right:10px; font-size:12px'>X</a><input type='hidden' name='relation[]' value='".$relation."'></div>";
			}
			echo $str1;
		} else {
			$str = <<<STR
				<table class="layui-table" lay-skin="line">
				  <colgroup>
				    <col width="100">
				    <col width="350">
				    <col width="100">
				    <col>
				  </colgroup>
				  <thead>
				    <tr>
				      <th>ID</th>
				      <th>标题</th>
				      <th>点击量</th>
				      <th>发布时间</th>
				    </tr>
				  </thead>
				  <tbody>
STR;
			$model = M('model')->getField('table',true);
			$arr = array();
			foreach ($model as $key => $value) {
				$tablename = 'model_'.$value;
				$list = M($tablename)->getField('id,modelid,title,addtime,hits',true);
				foreach ($list as $key1 => $value1) {
					$time = date("Y-m-d H:i:s",$value1['addtime']);
					$id = $value1['modelid'].'-'.$value1['id'];
					$str .=<<<STR
						<tr onclick="change('{$id}','{$value1['title']}')" id="show{$id}">
						  	<td>{$id}</td>
						 	<td>{$value1['title']}</td>
						  	<td>{$value1['hits']}</td>
						  	<td>{$time}</td>
						</tr>
STR;
				}
			}
			$str .= "</tbody></table>";
			echo $str;
		}
	}
}